//
//  HomeCell.m
//  WanCaiC8
//
//  Created by tangang on 2018/1/29.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "HomeCell.h"
#import "LotteryNumCell.h"
#import "AdvertisingView.h"
@interface HomeCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) UICollectionView *bottomCollectionView;

@property(nonatomic,strong)AdvertisingView *advertisingView;
@property(nonatomic,strong)UIView *lineView;
@end
@implementation HomeCell{
    UIImageView  *imageIcon;
    QXLable      *labName;
    QXLable      *labNum;
    QXLable      *labTime;
    Home_LotteryModel      *lotteryModel;
    dispatch_source_t _timer;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        WS(weakSelf);
        imageIcon =[[UIImageView alloc]init];
        imageIcon.layer.masksToBounds=YES;
        imageIcon.layer.cornerRadius=15;
        [self.contentView addSubview:imageIcon];
        [imageIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView).with.offset(13);
            make.size.mas_equalTo(CGSizeMake(30, 30));
            make.top.equalTo(weakSelf.contentView).with.offset(10);
        }];
        
        labName=[[QXLable alloc]initTitle];
        [self.contentView addSubview:labName];
        [labName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(imageIcon.mas_right).with.offset(13);
            make.centerY.equalTo(imageIcon);
            make.size.mas_equalTo(CGSizeMake(150, 40));
        }];
        
        UIView *lineView=[[UIView alloc]init];
        lineView.backgroundColor=DEFAULTBGCOLOR;
        [self.contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.contentView);
            make.top.equalTo(imageIcon.mas_bottom).with.offset(10);
            make.height.mas_equalTo(@.7);
        }];
        
        labNum =[[QXLable alloc]init];
        [self.contentView addSubview:labNum];
        [labNum mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView).with.offset(13);
            make.top.equalTo(lineView).with.offset(7.5);
            make.right.equalTo(weakSelf.contentView).with.offset(-13);
        }];
        self.collectionView.userInteractionEnabled=NO;
        [self.contentView addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labNum);
            make.right.equalTo(weakSelf.contentView);
            make.top.equalTo(labNum.mas_bottom).with.offset(10);
            make.height.mas_equalTo(@(real_size(27)));
        }];
        
//        [self.contentView addSubview:self.bottomCollectionView];
//        [self.bottomCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(labNum);
//            make.right.equalTo(weakSelf.contentView);
//            make.top.equalTo(weakSelf.collectionView.mas_bottom).with.offset(10);
//            make.height.mas_equalTo(@34);
//        }];
        
        UIView *line =[[UIView alloc]init];
        line.backgroundColor=DEFAULTBGCOLOR;
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.contentView);
            make.top.equalTo(weakSelf.collectionView.mas_bottom).with.offset(10);
            make.height.mas_equalTo(@5);
        }];
        
        
        self.advertisingView  = [[AdvertisingView alloc]init];
        self.advertisingView.changeHeight = ^(float height) {
            [weakSelf.advertisingView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(@(height));
            }];
        };
        [self.contentView addSubview:self.advertisingView];
        [self.advertisingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.contentView);
            make.top.equalTo(line.mas_bottom);
            make.height.mas_equalTo(@0);
        }];
        self.advertisingView.hidden= YES;
        
        self.lineView = [[UIView alloc] init];
        self.lineView.backgroundColor=DEFAULTBGCOLOR;
        [self.contentView addSubview:self.lineView];
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(weakSelf.contentView);
            make.top.equalTo(self.advertisingView.mas_bottom);
            make.height.mas_equalTo(@0);
        }];
        self.lineView.hidden= YES;
    }
    return self;
}

- (void)setModel:(Home_LotteryModel *)model{
    lotteryModel=model;
    [imageIcon sd_setImageWithURL:[NSURL URLWithString:model.Logo]];
    labName.text=model.LTypeName;
    NSMutableAttributedString *attStr=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"第%@期    下期开奖%@",model.Issue,model.OpenTime]];
    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(1, model.Issue.length)];
    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(attStr.length-model.OpenTime.length, model.OpenTime.length)];
    labNum.attributedText=attStr;
    [self.collectionView reloadData];
//    [self startTimer];
}




#pragma mark -- 启动定时器
- (void)startTimer{
    [self cancleTimer];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        if (![lotteryModel.OpenTime isEqualToString:@"正在开奖"]) {
            NSArray *timeArr=[lotteryModel.OpenTime componentsSeparatedByString:@":"];
            int hour=[timeArr[0] intValue];
            int minutes=[timeArr[1] intValue];
            int second=[timeArr[2] intValue];
            if (second>0) {
                second-=1;
                
                lotteryModel.OpenTime=[NSString stringWithFormat:@"%@:%@:%@",[TGTools getTime:hour],[TGTools getTime:minutes],[TGTools getTime:second]];
            }else if (minutes>0){
                minutes-=1;
                second=59;
                lotteryModel.OpenTime=[NSString stringWithFormat:@"%@:%@:%@",[TGTools getTime:hour],[TGTools getTime:minutes],[TGTools getTime:second]];
            }else if (hour>0){
                hour-=1;
                minutes=59;
                second=59;
                lotteryModel.OpenTime=[NSString stringWithFormat:@"%@:%@:%@",[TGTools getTime:hour],[TGTools getTime:minutes],[TGTools getTime:second]];
            }else{
                [self cancleTimer];
                lotteryModel.OpenTime=@"正在开奖";
            }
            
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            NSMutableAttributedString *attStr=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"第%@期    下期开奖%@",lotteryModel.Issue,lotteryModel.OpenTime]];
            [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(1, lotteryModel.Issue.length)];
            [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(attStr.length-lotteryModel.OpenTime.length, lotteryModel.OpenTime.length)];
            labNum.attributedText=attStr;
        });
        
    });
    dispatch_resume(_timer);
}

- (void)cancleTimer{
    if (_timer) {
        dispatch_source_cancel(_timer);
    }
}


#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView==self.collectionView) {
        if ([lotteryModel.LType integerValue]==65) {
            return lotteryModel.PCArr.count;
        }else if ([[BaseVM manage]getType:[lotteryModel.LType integerValue]]==LotteryNumTypeLottery){
            return lotteryModel.LotteryArr.count;
        }
        return lotteryModel.numArr.count;
    }
    return lotteryModel.results.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LotteryNumCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LotteryNumCell" forIndexPath:indexPath];
    if (collectionView==self.collectionView) {
        LotteryNumType type=[[BaseVM manage]getType:[lotteryModel.LType integerValue]];
        
        if ([lotteryModel.LType integerValue]==65) {
            [cell setTitle:lotteryModel.PCArr[indexPath.row] andType:type];
        }else if (type==LotteryNumTypeLottery){
            [cell setTitle:lotteryModel.LotteryArr[indexPath.row] andType:type];
        }else{
            [cell setTitle:lotteryModel.numArr[indexPath.row] andType:type];
        }
        
        
        if ((type==LotteryNumTypeChromosphere||type==LotteryNumTypeBrings||type==LotteryNumTypeSevenLecai)&&(indexPath.row==lotteryModel.numArr.count-1)) {
            [cell setBGColor:DEFAULTTEXTBLUECOLOR];
        }else if (type==LotteryNumTypeBrings&&(indexPath.row==lotteryModel.numArr.count-2)){
            [cell setBGColor:DEFAULTTEXTBLUECOLOR];
        }
    }else{
        [cell setDownTitle:lotteryModel.results[indexPath.row] andType:[[BaseVM manage]getType:[lotteryModel.LType integerValue]]];
        [cell setBGColor:DEFAULTBGCOLOR];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView==self.bottomCollectionView) {
        if (self.typeBlock) {
            self.typeBlock(indexPath.row);
        }
    }
}

#pragma mark -- 懒加载
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout =
        [[UICollectionViewFlowLayout alloc] init];
        [flowLayout setItemSize:CGSizeMake(real_size(27), real_size(27))];
        flowLayout.minimumLineSpacing=3;
        flowLayout.minimumInteritemSpacing=0;
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero
                                             collectionViewLayout:flowLayout];
        [_collectionView registerClass:[LotteryNumCell class] forCellWithReuseIdentifier:@"LotteryNumCell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

- (UICollectionView *)bottomCollectionView{
    if (!_bottomCollectionView) {
        UICollectionViewFlowLayout *flowLayout =
        [[UICollectionViewFlowLayout alloc] init];
        [flowLayout setItemSize:CGSizeMake(48, 34)];
        flowLayout.minimumLineSpacing=0;
        flowLayout.minimumInteritemSpacing=0;
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        _bottomCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero
                                             collectionViewLayout:flowLayout];
        [_bottomCollectionView registerClass:[LotteryNumCell class] forCellWithReuseIdentifier:@"LotteryNumCell"];
        _bottomCollectionView.delegate = self;
        _bottomCollectionView.dataSource = self;
        _bottomCollectionView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomCollectionView;
}



-(void)setModelArr:(NSMutableArray<AdvertisingModel *> *)modelArr{
    _modelArr = modelArr;
    if (modelArr.count>0) {
        self.advertisingView.dataArr = modelArr;
        
        self.advertisingView.hidden = NO;
        self.lineView.hidden = NO;
        
        self.advertisingView.changeHeight(modelArr.count*40);
        
        [self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(5);
        }];
        
    }else{
        self.advertisingView.hidden = YES;
        self.lineView.hidden = YES;
       
        
        self.advertisingView.changeHeight(0);
    }
}

@end
